/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.Dao.ArchiveImpl;
import com.lcfgly.Database.ShopArchives;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Plug.DataTableForShoprice;
import java.sql.SQLException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author lcfgly
 */
@Service
public class ArchivesService {
    
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    ArchiveImpl archiveImpl;
    
    @Autowired
    DataTableForShoprice datatable;
    
    /**
     * 保存文章
     * @param archives
     * @throws ActionException 
     */
    public void create(ShopArchives archives) throws ActionException{
        try{
            int x=archiveImpl.insert(archives);
            if(x != 1){
                throw new ActionException("数据保存失败！");
            }
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取栏目列表（DataTable插件）数据
     * @param request
     * @param typeid 栏目ID
     * @param userid 用户ID
     * @return
     * @throws ActionException 
     */
    public Map GetMenuListForDataTable(HttpServletRequest request , int typeid,int userid) throws ActionException{
        try {
            return datatable.GetDataForDataTable(request, "shop_archives", "id,title,updatetime",null, "typeid="+typeid+" AND writeruid="+userid, null,null);
        } catch (SQLException ex) {
            log.error(ex);
            return null;
        }
    }
    
    /**
     * 获取文章信息
     * @param id
     * @param userid
     * @return
     * @throws ActionException 
     */
    public ShopArchives findByIdAndUserid(int id,int userid) throws ActionException{
        try{
            ShopArchives arc=archiveImpl.findById(id);
            if(arc.getWriteruid() != userid){
                throw new ActionException("未查找到先关内容!");
            }
            return arc;
        }catch(SQLException e){
            log.error(e);
            throw new ActionException("未查找到先关内容!");
        }
    }
    
    /**
     * 保存文章修改
     * @param archives
     * @throws ActionException 
     */
    public void save(ShopArchives archives) throws ActionException {
        try{
            int x=archiveImpl.update(archives);
            if(x != 1){
                throw new ActionException("数据保存失败！");
            }
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 删除文章
     * @param id
     * @param userid
     * @return 
     */
    public boolean del(int id,int userid){
        try{
            int x=archiveImpl.delbyIdAndUserid(id, userid);
            return x==1;
        }catch(SQLException e){
            log.error(e);
            return false;
        }
    }
    
    /**
     * 获取文章分页
     * @param shopid
     * @param typeid
     * @param page
     * @return 
     */
    public Map GetArchivesForPage(int shopid,int typeid,int page){
        try{
           return archiveImpl.GetArchivesForPage(shopid, typeid, page);
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
}
